package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.util.DateUtils;
import com.youlai.system.converter.CarSysDescConverter;
import com.youlai.system.mapper.CarSysDescMapper;
import com.youlai.system.model.entity.TCarSysDesc;
import com.youlai.system.model.form.SysDescForm;
import com.youlai.system.model.query.CarSysDescPageQuery;
import com.youlai.system.model.vo.CarSysDescPageVO;
import com.youlai.system.service.CarSysDescService;
import com.youlai.system.service.SysMenuService;
import com.youlai.system.service.SysRoleService;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 模板业务实现类
 *
 * @author haoxr
 * @since 2022/1/14
 */
@Service
@RequiredArgsConstructor
public class CarSysDescServiceImpl extends ServiceImpl<CarSysDescMapper, TCarSysDesc> implements CarSysDescService {


    private final CarSysDescConverter carSysDescConverter;

    /**
     * 获取模板分页列表
     * @param queryParams
     * @return
     */
    @Override
    public IPage<CarSysDescPageVO> getSysDescPage(CarSysDescPageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<CarSysDescPageVO> page = new Page<>(pageNum, pageSize);
        // 格式化为数据库日期格式，避免日期比较使用格式化函数导致索引失效
        if(StringUtils.isNotEmpty(queryParams.getStartTime()) && StringUtils.isNotEmpty(queryParams.getEndTime())) {
            DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");
        }
        // 查询数据
        Page<CarSysDescPageVO> userPage = this.baseMapper.selectCarSysDescPage(page, queryParams);
        // 实体转换
        return userPage;
    }

    /**
     * 更新模板
     * @param sysDescId   模板ID
     * @param sysDescForm 模板表单对象
     * @return
     */
    @Override
    @Transactional
    public boolean  saveSysDesc(Integer sysDescId, SysDescForm sysDescForm) {

        // form -> entity
        TCarSysDesc entity = carSysDescConverter.form2Entity(sysDescForm);
        // 修改模板
        boolean result=  this.saveOrUpdate(entity);

        return result;
    }

    /**
     * 删除模板
     *
     * @param idsStr 模板ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteSysDesc(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的模板数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return this.removeByIds(ids);

    }

    @Override
    public Integer getSysDescInfo() {
        return  this.baseMapper.selectMaxSysDescInfo();
    }


}
